package main

import (
	"app_log_server/app"
	"app_log_server/http/routes"
	"flag"
	"log"
	"path"
	"runtime"

	"github.com/gin-gonic/gin"
)

func main() {
	initProjectArgs()

	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	routes.RegisterRoutes(r)
	err := r.Run(app.Config.Http.Listen)
	if err != nil {
		log.Fatal(err)
	}
}

func initProjectArgs() {
	_homePath := flag.String("app_home", ".", "当前项目的home目录")
	if *_homePath == "" || *_homePath == "." {
		_, f, _, ok := runtime.Caller(0)
		if ok {
			err := app.Init(path.Dir(f))
			if err != nil {
				panic(err)
			}
		} else {
			panic("获取当前项目路径失败")
		}
	}
	flag.Parse()
}
